package com.atguigu.processor;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

@Component
public class RedisTemplateBeanPostProcessor implements BeanPostProcessor {
    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        if(RedisTemplate.class.isAssignableFrom(bean.getClass())){
            RedisTemplate redisTemplate = (RedisTemplate) bean;
            //将redisTemplate的key的序列化规则设置为StringRedisSerializer
            redisTemplate.setKeySerializer(new StringRedisSerializer());
            return redisTemplate ;
        }
        return bean;
    }
}
